//
//  TextImageViewAttachment.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/2/21.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "TextImageViewAttachment.h"
#import "STPhotoBroswer.h"


@implementation TextImageViewAttachment {
    UIImageView *_imageView;
}

- (void)setContent:(id)content {
    _imageView = content;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    _imageView.userInteractionEnabled = YES;
    [_imageView addGestureRecognizer:tap];
}

- (id)content {
    /// UIImageView 只能在主线程访问
    if (pthread_main_np() == 0) return nil;
    if (_imageView) return _imageView;
    
    /// 第一次获取时 (应该是在文本渲染完成，需要添加附件视图时)，初始化图片视图，并下载图片
    /// 这里改成 YYAnimatedImageView 就能支持 GIF/APNG/WebP 动画了
    _imageView = [UIImageView new];
    _imageView.size = _size;
    [_imageView setImageWithURL:_imageURL placeholder:nil];
    return _imageView;
}
- (void)tap:(UITapGestureRecognizer*)tap{

    if (!_imageView.image) {//解决图片url出错情况下，未下载到图片的崩溃问题
        return;
    }
    NSArray *imageArray = @[_imageView.image];
    STPhotoBroswer * broser = [[STPhotoBroswer alloc]initWithImageArray:imageArray currentIndex:0];
    [broser show];
}

@end
